/**
 * 控制台页面
 */
import { Space, Table, Tag, Popover, Card } from 'antd';
import dayjs from 'dayjs';
import { detailList } from '../../api/data-api';
import { useEffect, useState } from 'react';
import moment from 'moment';

const timeStdFormat = (time)=>{
  return moment(time).format("YYYY-MM-DD")
}

export { 
  timeStdFormat
}

const columns = [
  {
    title: '标题',
    dataIndex: 'title',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
  {
    title: '作者',
    dataIndex: 'authors',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
  {
    title: '摘要',
    dataIndex: 'digest',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
  {
    title: '来源',
    dataIndex: 'college',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
  {
    title: '下载次数',
    dataIndex: 'downloadCount',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },

  
  {
    title: '主题',
    dataIndex: 'subject',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
  {
    title: '类别',
    dataIndex: 'searchKeyword',
    ellipsis: true,
    width: 120,
    render: (text) => {
      return <>
        <Popover content={text}>
          {text}
        </Popover>
      </>
    }
  },
];


export default function DashBoard(props){

  const [datasource, setDataSource] = useState([])
  const [total, setTotal]=  useState(0)

  const [condition, setCondition] = useState({
      "page": 1,
      "page_size": 40,
      "type": "",
      "keywords": "",
      "start_time": dayjs(new Date("2023-1-1")),
      "end_time": dayjs(new Date())
  })

  const loadData = (param)=>{
    if(!param){param = {}}
    const newParam = {...condition, ...param}
    newParam.start_time = timeStdFormat(newParam.start_time)
    newParam.end_time = timeStdFormat(newParam.end_time)
    detailList(newParam).then(
      resp=>{
        const data = resp.data.data
        const { list, total, page, pageSize} = data
        setTotal(total)
        setDataSource(list)
        
      }
    )
  }

  useEffect(()=>{
    loadData()
  }, [condition])

  return <>
    <Card title={"论文详细信息"}>
      <Table columns={columns} dataSource={datasource} pagination={{
        total: total,
        showTotal: (total, range)=>{
          return "共" + total + "条"
        },
        pageSize: condition.page_size,
        page: condition.page,
        onChange: (page, pageSize)=>{
          // loadData({page, pageSize})
          setCondition({...condition, page, pageSize})
        }
      }} 
        scroll={{
          y: '60vh'
        }}
      />
    </Card>
  </>
}